import Analysis.General
import Analysis.StatisticTest
import Analysis.PreProcess
import Analysis.IndustryAnalysis
import Factors
import Performance.FactorModel
import Core.Gadget as Gadget
import Core.IO as IO
import Core.Algorithm as Algorithm
import datetime
import pandas as pd
import math
import copy
import matplotlib.pyplot as plt
import numpy as np

# 已知return结果，看Factor差异
def Return_to_Factor_DiffSignificantTest(database, datetime1, datetime2):
    #
    pathName = "d://data//FactorAnalysis//"
    #
    # dfReturns = Analysis.General.RangeReturn(database, datetime1, datetime2)
    # dfReturns.to_csv(pathName + "Return_" + Gadget.ToDateString(datetime1) + "_" + Gadget.ToDateString(datetime2) + ".csv")
    dfReturns = pd.read_csv(pathName + "Return_" + Gadget.ToDateString(datetime1) + "_" + Gadget.ToDateString(datetime2) + ".csv", engine="python")

    # Exclude New Stocks
    datetime0 = datetime1 + datetime.timedelta(days=-365)
    instruments = Gadget.FindListedInstrument(database, datetime0)
    dfSymbols = Gadget.InstrumentToDateFrame(instruments)

    #
    dfReturns = pd.merge(dfSymbols, dfReturns, on="Symbol", how="left")

    #
    dfReturns.sort_values(by="Return", ascending=False)
    count = dfReturns.shape[0]
    topCount = int(count*0.1)
    dfTopPortfolio = dfReturns[0:topCount]
    dfBotPortfolio = dfReturns[topCount:]

    #
    factorInDatabase = database.GetCollectionNames("Factor")

    #
    factors = Factors.FactorParams()

    #
    data = []
    for factorName, param in factors.items():
        #
        if factorName.lower() not in factorInDatabase:
            print(factorName, "Not Exist")
            continue
        #
        dfFactor = Analysis.General.Profile(database, datetime1, [factorName], period=param["Period"])
        if len(dfFactor) < 100:
            print("Missing Factor", factorName)
            continue
        #
        Analysis.PreProcess.Outlier_Percentile(dfFactor)
        # print(dfFactor.head())

        # Good vs Bad
        dfFactorTop = pd.merge(dfTopPortfolio, dfFactor, on="Symbol", how="inner")
        dfFactorBot = pd.merge(dfBotPortfolio, dfFactor, on="Symbol", how="inner")
        t = Analysis.StatisticTest.TwoSampleTTest(dfFactorTop[factorName], dfFactorBot[factorName])
        print(factorName, t)
        data.append([factorName, t, math.fabs(t)])
    #
    df = pd.DataFrame(data, columns=["Factor", "T", "AbsT"])
    df.sort_values(by="AbsT", ascending=False, inplace=True)
    df.to_csv(pathName + "ReturnToFactorDiffTTest_" + Gadget.ToDateString(datetime1) + "_" + Gadget.ToDateString(datetime2) + ".csv")


# 根据数据库返回全部因子
def TotalFactors():
    #
    factorInDatabase = database.GetCollectionNames("Factor")
    factorParams = Factors.FactorParams()
    #
    factorList = []
    #
    # ---全部因子---
    for name in factorParams.keys():
        if name.lower() in factorInDatabase:
            factorList.append(name)
    #
    factors = {}
    for name in factorList:
        factors[name] = factorParams.get(name)
    #
    return factors


def TestSomeFactors():
    factorList = []
    factorList.append("Holding_MF_Chg_LYR")
    factorList.append("Holding_MF_Chg_YoY")
    factorList.append("Holding_MF_Diff_LYR")
    factorList.append("Holding_MF_Diff_YoY")
    #
    lowerFactorList = []
    for n in factorList:
        lowerFactorList.append(n.lower())
    #
    factors = {}
    #
    factorParams = Factors.FactorParams()
    for name in factorParams.keys():
        if name.lower() in lowerFactorList:
             factors[name] = factorParams.get(name)
    #
    return factors

# FactorPortfolio Hi-Lo TTest
# 计算因子组合
# 储存因子组合
def Calc_FactorPortfolioReturn(database, datetime1, datetime2, factorList=[],
                               diffType = "",
                               save=False, stat=True, filename=""):
    #
    if len(factorList) == 0:
        # ---全部因子---
        factorInDatabase = database.GetCollectionNames("Factor")
        factorParams = Factors.FactorParams()
        for name in factorParams.keys():
            if name.lower() in factorInDatabase:
                factorList.append(name)
                # if len(name) > maxLength:
                #     maxLength = len(name)
                #     maxLengthText = name
    else:
        factorParams = Factors.FactorParams()
        factors = {}
        for name in factorList:
            p = factorParams.get(name)
            if p == None:
                print(name, "Can't Find Factor Params")
            factors[name] = p
    #
    factors = {}
    for name in factorList:
        factors[name] = factorParams.get(name)

    params = {}
    params["GroupType"] = "Percentile"
    Performance.FactorModel.BuildFactorPortfolio(database, datetime1, datetime2, factors, params, save)
    #
    Export_RangeReturn(factors, datetime1, datetime2)

    # ---Statistics---
    if stat:
        Statistic_Factor(factors, database, datetime1, datetime2, filename)

    # ---Save Factor to Database---
    # if save:
    #     saveParams = {}
    #     saveParams["GroupType"] = params.get("GroupType")
    #     Save_FactorPortfolio_WithContent(database, factors, saveParams)


def Export_RangeReturn(dataByFactorName, datetime1, datetime2):
    #
    pathName = "d://data//FactorAnalysis//"
    #
    for factorName, value in dataByFactorName.items():
        #
        # ---Export RangeReturn---
        if value.get("Portfolio"):
            dfRangeReturn0 = value["Portfolio"][0]["RangeReturn"]
            # dfRangeReturn0.sort_values(by=["DateTime1", "RangeReturn"], inplace=True, ascending=[True, False])
            dfRangeReturn0.to_csv(pathName + factorName + "//" + factorName + "_RangeReturn_Hi_" + Gadget.ToDateString(
                datetime1) + "_" + Gadget.ToDateString(datetime2) + ".csv")
            #
            dfRangeReturn1 = value["Portfolio"][-1]["RangeReturn"]
            # dfRangeReturn1.sort_values(by=["DateTime1", "RangeReturn"], inplace=True, ascending=[True, False])
            dfRangeReturn1.to_csv(pathName + factorName + "//" + factorName + "_RangeReturn_Lo_" + Gadget.ToDateString(
                datetime1) + "_" + Gadget.ToDateString(datetime2) + ".csv")


# 对组合进行 TTest
def Statistic_Factor(factors, database, datetime1=None, datetime2=None, filename="", compareType="TopToBot"):
    if compareType == "TopToBot":
        DiffSignificantTest_TopToBot(factors, database, datetime1, datetime2, filename)
    elif compareType == "PortfolioToMarket":
        DiffSignificantTest_PortfolioToMarket(factors, database, datetime1, datetime2, filename)


# Top组 vs Bot组 T-Test
def DiffSignificantTest_TopToBot(factors, database, datetime1=None, datetime2=None, filename=""):
    #
    pathName = "d://data//FactorAnalysis//"

    #
    data = []
    for factorName, value in factors.items():
        # ---
        filter = []
        filter.append(["FactorName", factorName])
        filter.append(["GroupType", "Percentile"])

        # ---Export DataSeries Diff---
        if value.get("Portfolio"):
            dataSeries0 = value["Portfolio"][0]["DataSeries"]
            df0 = pd.DataFrame(dataSeries0, columns=["DateTime", "Hi"])
            # 以缓存中的数据为准，更新Datetime
            if len(dataSeries0) == 0:
                print(factorName, "Missing DateTime")
                continue
            datetime1 = dataSeries0[0][0]
            datetime2 = dataSeries0[-1][0]
            #
            dataSeries1 = value["Portfolio"][-1]["DataSeries"]
            df1 = pd.DataFrame(dataSeries1, columns=["DateTime", "Lo"])
        else:
            #
            if datetime1 == None or datetime2 == None:
                print("Statistic_Factor Missing DateTime1 or DateTime2")
                return

            filter.append(["DateTime", ">=", datetime1])
            filter.append(["DateTime", "<=", datetime2])
            #
            filter0 = copy.deepcopy(filter)
            filter0.append(["Number", 0])
            dataSeries0 = database.Find("Portfolio", "Factor_Portfolio", filter0)
            df0 = Gadget.DocumentsToDataFrame(dataSeries0, {"Value": "Hi"}, keep=["DateTime", "Value"])
            #
            filter9 = copy.deepcopy(filter)
            filter9.append(["Number", 9])
            dataSeries1 = database.Find("Portfolio", "Factor_Portfolio", filter9)
            df1 = Gadget.DocumentsToDataFrame(dataSeries1, {"Value": "Lo"}, keep=["DateTime", "Value"])
        #
        if len(dataSeries0) == 0:
            print(factorName, "No TimeSeries Data")
            continue

        # --- Merge to Test ---
        df = pd.merge(df0, df1, on="DateTime", how="inner")
        df["Hi_NetValue"] = pd.DataFrame.cumprod(df["Hi"] + 1)
        df["Lo_NetValue"] = pd.DataFrame.cumprod(df["Lo"] + 1)
        df[factorName] = df["Hi"] - df["Lo"]
        #

        t = Analysis.StatisticTest.PairedTest(df["Hi"], df["Lo"])
        print(factorName, t)
        data.append([factorName, t, math.fabs(t)])
        pass
    #
    df = pd.DataFrame(data, columns=["Factor", "T", "AbsT"])
    df.sort_values(by="AbsT", ascending=False, inplace=True)
    df.to_csv(pathName + "FactorPortfolioReturn_TTest_" + filename + "_" + Gadget.ToDateString(
        datetime1) + "_" + Gadget.ToDateString(
        datetime2) + ".csv")


# 因子组合和Market组合做T-test
def  DiffSignificantTest_PortfolioToMarket(factors, database, datetime1=None, datetime2=None, filename="", export=False):
    #
    pathName = "d://data//FactorAnalysis//"
    #
    filter = []
    filter.append(["DateTime", ">=", datetime1])
    filter.append(["DateTime", "<=", datetime2])
    #
    filterMarket = copy.deepcopy(filter)
    filterMarket.append(["PortfolioName", "TotalA_EqualWeight"])
    dataSeriesMarket = database.Find("Portfolio", "Market_Portfolio", filterMarket)
    dfMarket = Gadget.DocumentsToDataFrame(dataSeriesMarket, keep=["DateTime", "DailyReturn"])
    dfMarket.rename(columns={"DailyReturn": "MarketReturn"}, inplace=True)
    #
    data = []
    for factorName, value in factors.items():
        #
        filter = []
        filter.append(["DateTime", ">=", datetime1])
        filter.append(["DateTime", "<=", datetime2])
        filter.append(["GroupType", "Percentile"])
        filter.append(["FactorName", factorName])
        #
        filter0 = copy.deepcopy(filter)
        filter0.append(["Number", 0])
        dataSeries0 = database.Find("Portfolio", "Factor_Portfolio", filter0)
        dfHi = Gadget.DocumentsToDataFrame(dataSeries0, keep=["Date", "Value"], rename={"Date": "DateTime", "Value": "Hi"})
        #
        filter9 = copy.deepcopy(filter)
        filter9.append(["Number", 9])
        dataSeries1 = database.Find("Portfolio", "Factor_Portfolio", filter9)
        dfLo = Gadget.DocumentsToDataFrame(dataSeries1, keep=["Date", "Value"], rename={"Date": "DateTime", "Value": "Lo"})
        #
        if len(dataSeries0) == 0 or len(dataSeries1) == 0:
            print(factorName, "No TimeSeries Data")
            continue

        # ---Fix DateTime---
        dfHi["DateTime"] = pd.to_datetime(dfHi["DateTime"])
        dfLo["DateTime"] = pd.to_datetime(dfLo["DateTime"])

        # --- Merge to Test ---
        df = pd.merge(dfMarket, dfHi, on="DateTime", how="left")
        df.dropna(inplace=True)
        tHi = Analysis.StatisticTest.PairedTest(df["Hi"], df["MarketReturn"])

        #
        df = pd.merge(dfMarket, dfLo, on="DateTime", how="left")
        df.dropna(inplace=True)
        tLo = Analysis.StatisticTest.PairedTest(df["Lo"], df["MarketReturn"])

        print(factorName, tHi, tLo)
        data.append([factorName, "Hi", tHi, math.fabs(tHi)])
        data.append([factorName, "Lo", tLo, math.fabs(tLo)])
        # pass

    df = pd.DataFrame(data, columns=["Factor", "Group", "T", "AbsT"])
    df.sort_values(by="AbsT", ascending=False, inplace=True)

    if export:
        df.to_csv(pathName + "FactorPortfolioReturn_ToMarket_TTest_" + filename + "_" + Gadget.ToDateString(
            datetime1) + "_" + Gadget.ToDateString(
            datetime2) + ".csv")
    #
    return df


# 检查数据库因子 和 因子列表差异
def CheckMissingFactor(database):
    #
    names = database.GetCollectionNames("Factor")
    factors = Factors.FactorParams()
    #
    print("Factor Not included in Content")
    for name in factors.keys():
        name = name.lower()
        if name in names:
            names.remove(name)
        else:
            print(name)
            pass
    #
    # print(names)
    print("")
    print("Factor Not to Test")
    for name in names:
        print(name)


def Test():
    pass
    data1 = [[10,20,30], [11,21,31], [12,22,32]]
    df1 = pd.DataFrame(data1, columns=["One", "Two", "Three"])
    #
    data2 = [[10,40,30], [11,41,31], [12,42,32]]
    df2 = pd.DataFrame(data2, columns=["One", "Four", "Three"])
    #
    df = pd.concat([df1,df2])
    print(df)


# Plot Hi Lo Group
def Plot_FatorPortfolio(database, factorName, datetime1=None, datetime2=None, diffType="TopToBot"):

    if datetime1 == None:
        datetime1 = datetime.datetime(2000,1,1)
    if datetime2 == None:
        datetime2 = datetime.datetime.now()

    filter = {}
    filter["FactorName"] = factorName
    filter["Date"] = {">=": datetime1.date(), "<=": datetime2.date()}
    filter["GroupType"] = "Percentile"
    filter["Number"] = 0
    #
    dataSeries0 = database.Find("Portfolio", "Factor_Portfolio", filter, sort=[("Date",1)])
    dfPf0 = Gadget.DocumentsToDataFrame(dataSeries0, keep=["Date", "Value"], rename={"Date": "Date", "Value": "Return"})
    print(dfPf0)
    dfPf0["NetValue"] = pd.DataFrame.cumprod(dfPf0["Return"] + 1)
    dfPf0.rename(columns={"NetValue": "NetValue0"}, inplace=True)
    #
    filter["Number"] = 9
    dataSeries9 = database.Find("Portfolio", "Factor_Portfolio", filter, sort=[("Date",1)])
    dfPf9 = Gadget.DocumentsToDataFrame(dataSeries9, keep=["Date", "Value"], rename={"Date": "Date", "Value": "Return"})
    dfPf9["NetValue"] = pd.DataFrame.cumprod(dfPf9["Return"] + 1)
    dfPf9.rename(columns={"NetValue": "NetValue9"}, inplace=True)

    df = pd.merge(dfPf0, dfPf9, on="Date", how="inner")
    df["Diff"] = df["NetValue0"] - df["NetValue9"]

    # print(df)

    ax1 = plt.subplot(211)
    df.plot(x="Date", y=["NetValue0", "NetValue9"], grid=True, ax=ax1, title=factorName)
    # ax1.set_ylabel('Net Unit Value')

    ax2 = plt.subplot(212)
    df.plot(x="Date", y=["Diff"], grid=True, ax=ax2)
    # ax2.set_ylabel('Cumulative Excess Return')

    plt.show()

    pass


# Plot Factor Portfolio vs Market
def Plot_FatorPortfolio_ToMarkt(database, factorName, datetime1=None, datetime2=None, benchmark=""):

    if datetime1 == None:
        datetime1 = datetime.datetime(2000,1,1)
    if datetime2 == None:
        datetime2 = datetime.datetime.now()

    # Common Filter
    filter = {}
    filter["Date"] = {">=": datetime1.date(), "<=": datetime2.date()}

    filterMarket = copy.deepcopy(filter)
    filterMarket["PortfolioName"] = "TotalA_EqualWeight"
    dataSeriesMarket = database.Find("Portfolio", "Market_Portfolio", filterMarket, sort=[("Date",1)])
    dfMarket = Gadget.DocumentsToDataFrame(dataSeriesMarket, keep=["Date", "DailyReturn"])
    dfMarket.rename(columns={"DailyReturn": "MarketReturn"}, inplace=True)
    dfMarket["Market"] = pd.DataFrame.cumprod(dfMarket["MarketReturn"] + 1)

    # ---Hi Group---
    filter["FactorName"] = factorName
    filter["GroupType"] = "Percentile"
    #
    filter["Number"] = 0
    dataSeriesHi = database.Find("Portfolio", "Factor_Portfolio", filter, sort=[("Date",1)])
    #
    if len(dataSeriesHi) == 0:
        print(factorName, "No TimeSeries Data")
        return

    dfHi = Gadget.DocumentsToDataFrame(dataSeriesHi, keep=["Date", "Value"], rename={"Value": "Return"})
    dfHi["NetValueHi"] = pd.DataFrame.cumprod(dfHi["Return"] + 1)
    # Merge to Market
    dfHi = pd.merge(dfMarket, dfHi, on="Date", how="inner")
    dfHi["ExcessReturn"] = dfHi["NetValueHi"] - dfHi["Market"]

    # ---Lo Group---
    filter["Number"] = 9
    dataSeriesLo = database.Find("Portfolio", "Factor_Portfolio", filter, sort=[("Date",1)])
    dfLo = Gadget.DocumentsToDataFrame(dataSeriesLo, keep=["Date", "Value"], rename={"Value": "Return"})
    dfLo["NetValueLo"] = pd.DataFrame.cumprod(dfLo["Return"] + 1)
    # Merge to Market
    dfLo = pd.merge(dfMarket, dfLo, on="Date", how="inner")
    dfLo["ExcessReturn"] = dfLo["NetValueLo"] - dfLo["Market"]
    # print(df)

    ax1 = plt.subplot(221)
    dfHi.plot(x="Date", y=["NetValueHi", "Market"], grid=True, ax=ax1, title=factorName + " Hi")
    # ax1.set_ylabel('Net Unit Value')

    ax2 = plt.subplot(222)
    dfLo.plot(x="Date", y=["NetValueLo", "Market"], grid=True, ax=ax2, title=factorName + " Lo")

    ax3 = plt.subplot(223)
    dfHi.plot(x="Date", y=["ExcessReturn"], grid=True, ax=ax3, title=factorName + " Excess Return Hi")
    # ax2.set_ylabel('Cumulative Excess Return')

    ax4 = plt.subplot(224)
    dfLo.plot(x="Date", y=["ExcessReturn"], grid=True, ax=ax4, title=factorName + " Excess Return Lo")
    plt.show()

    pass


def TestFactors_MFHolding(database):
    #
    factorList = []
    factorList.append("Holding_MF_Chg_LYR")
    factorList.append("Holding_MF_Chg_YoY")
    factorList.append("Holding_MF_Diff_LYR")
    factorList.append("Holding_MF_Diff_YoY")
    factorList.append("Holding_MF_RatioChg_LYR")
    factorList.append("Holding_MF_RatioChg_YoY")
    factorList.append("Holding_MF_RatioChg_LF")
    factorList.append("Holding_MF_RatioDiff_LYR")
    factorList.append("Holding_MF_RatioDiff_YoY")
    factorList.append("Holding_MF_RatioDiff_LF")

    datetime1 = datetime.datetime(2010, 1, 5)
    datetime2 = datetime.datetime(2019, 9, 6)

    Calc_FactorPortfolioReturn(database,
                               datetime1, datetime2,
                               factorList=factorList,
                               save=True, stat=True, filename="HoldingMF")


def Fix_FactorPortfolio(database, datetime1, datetime2):
    factors = TotalFactors()
    #
    for factorName, params in factors.items():
        filter = {}
        filter["FactorName"] = factorName
        filter["GroupType"] = "Percentile"
        filter["Number"] = 0
        dataSeries = database.Find("Portfolio", "Factor_Portfolio", filter, sort=[("Date",1)])
        #
        if len(dataSeries) == 0:
            print("No Factor Portfolio of", factorName)
            return
        #
        print(factorName, dataSeries[0]["Date"], dataSeries[-1]["Date"])
        if dataSeries[0]["Date"] > datetime1.date():
            print("")
            print("Rebuild", factorName, datetime1, dataSeries[0]["Date"], datetime.datetime.now())
            Calc_FactorPortfolioReturn(database, datetime1, dataSeries[0]["Date"], factorList=[factorName], save=True, stat=False)


# ---模拟测试 Factor Strategy---
def Factor_Strategy(database, datetime1, datetime2):
    #
    def Calc_Performance(database, datetime1, datetime2, factorwDirectList):
        filter = {}
        filter["Date"] = {">=":datetime1, "<=": datetime2}
        filter["Symbol"] = "000001.SH"
        dataSeries = database.Find("DailyBar", "Index", filter=filter, sort=[("Date", 1)])
        df = Gadget.DocumentsToDataFrame(dataSeries, keep=["Date", "Close"])

        for factorName, value in factorwDirectList.items():
            filter = {}
            filter["Date"] = {">=": datetime1, "<=": datetime2}
            filter["FactorName"] = factorName
            filter["GroupType"] = "Percentile"
            if value["Group"] == "Hi":
                filter["Number"] = 0
            else:
                filter["Number"] = 9

            dataSeries = database.Find("Portfolio", "Factor_Portfolio", filter, sort=[("Date", 1)])
            dfFactor = Gadget.DocumentsToDataFrame(dataSeries, keep=["Date", "Value"])
            # if filter["T"] < 0:
            #     dfFactor["Value"] = -1 * dfFactor["Value"]
            #
            if len(dataSeries) == 0:
                print("No Factor Portfolio of", factorName)
                continue
            #
            df = pd.merge(df, dfFactor, how="left", on="Date")
        #
        if len(factorwDirectList) == 0:
            df["Return"] = 0
        else:
            # print(df)
            df.drop(columns=["Close"], inplace=True)
            dfMean = df.mean(axis=1)
            df["Return"] = dfMean
        #
        # print(df)
        return df[["Date", "Return"]]

    factors = TotalFactors()
    # factors = TestSomeFactors()
    holdFactors = {}
    dfReturns = pd.DataFrame()
    lastDateTime = None
    for curDateTime in Gadget.GenerateMonthDates(datetime1, datetime2):
        print("### ###")
        print(curDateTime)
        # ---Calc Performance---
        if lastDateTime != None:
            lastDateTime = lastDateTime + datetime.timedelta(days=1)
            dfRangeReturn = Calc_Performance(database, lastDateTime, curDateTime, holdFactors)
            #
            if dfReturns.empty:
                dfReturns = dfRangeReturn
            else:
                dfReturns = pd.concat([dfReturns, dfRangeReturn])
            #
            # print(dfReturns)
            a = 0
        # ---Find most valuable Factor---
        preDateTime = curDateTime + datetime.timedelta(days=-90)
        dfTTest = DiffSignificantTest_PortfolioToMarket(factors, database, preDateTime, curDateTime)

        # ---Select Factors: T > 2, top 3 Factors---
        # dfTTest = dfTTest[dfTTest["AbsT"] > 2] # permit Short
        # dfTTest = dfTTest[dfTTest["T"] > 2]
        dfTTest = dfTTest[dfTTest["T"] < -2]

        if len(dfTTest) == 0:
            holdFactors = {}
        else:
            factorCount = 0
            dfTTest.sort_values(by="AbsT", ascending=False, inplace=True)
            print(dfTTest)
            for index, row in dfTTest.iterrows():
                factorCount += 1
                factorName = row["Factor"]
                holdFactors[factorName] = {"Group": row["Group"], "T": row["T"]}
                if factorCount >= 3:
                    break

        # ---Build Portfolio---
        lastDateTime = curDateTime

    # ---Add Benchmark---
    filter = {}
    filter["Date"] = {">=": datetime1, "<=": datetime2}
    filter["Symbol"] = "000001.SH"
    dataSeries = database.Find("DailyBar", "Index", filter=filter, sort=[("Date", 1)])
    dfBM = Gadget.DocumentsToDataFrame(dataSeries, keep=["Date", "Close"])
    #
    dfReturns["NetValue"] = pd.DataFrame.cumprod(dfReturns["Return"] + 1)
    dfReturns = pd.merge(dfReturns, dfBM, how="left", on="Date")
    #
    beginValue = dfReturns.iloc[0]["Close"]
    dfReturns["Benchmark"] = dfReturns["Close"] / beginValue
    #
    dfReturns.to_csv("d:/Data/FactorStrategy/Netvalues.csv")
    pass


if __name__ == '__main__':
    #
    from Core.Config import *
    cfgPathFilename = os.getcwd() + "/../config.json"
    config = Config(cfgPathFilename)
    database = config.DataBase("MySQL")
    realtime = config.RealTime()

    # ---Test Strategy---
    datetime1 = datetime.datetime(2017, 1, 1)
    datetime2 = datetime.datetime(2019, 11, 1)
    # Factor_Strategy(database, datetime1, datetime2)

    # ---构建等权重指数---
    datetime1 = datetime.datetime(2016, 1, 1)
    datetime2 = datetime.datetime(2019, 11, 27)
    # Analysis.IndustryAnalysis.BuildEquallyWeightIndex(database, datetime1, datetime2, saveToDateBase=True)

    # ---Calculate Factor Portfolio Return and Save---
    datetime1 = datetime.datetime(2019, 1, 1)
    datetime2 = datetime.datetime(2020, 1, 8)
    # Calc_FactorPortfolioReturn(database, datetime1, datetime2, factorList=[], save=True, stat=False)
    #
    factorList = []
    factorList.append("ROE_NetIncome2_TTM_Neutral")
    #
    factorList.append("PE_NetIncome2_TTM_Neutral")
    factorList.append("EarningToPrice_TTM")
    factorList.append("PS_TTM")
    factorList.append("PCF_TTM")
    factorList.append("PCF_R_TTM")
    #
    factorList.append("CashEarningToPrice_TTM")
    factorList.append("CashROIC_TTM")
    factorList.append("CashEarningMinusEarning_TTM")
    #
    factorList.append("Growth_CAGR_NetIncome2_3Yr")
    factorList.append("Growth_CAGR_TotalRevenue_3Yr")
    factorList.append("Growth_CAGR_TotalAsset_3Yr")
    factorList.append("Growth_YoY_TotalRevenue")
    factorList.append("Growth_YoY_NetIncome2")
    #
    factorList.append("DebtToAsset")
    #
    factorList.append("AssetTurnover_TTM")

    Calc_FactorPortfolioReturn(database, datetime1, datetime2, factorList=factorList, save=True, stat=False)

    # Fix_FactorPortfolio(database, datetime1, datetime2)

    # ---T Test for Recent Month---
    datetime1 = datetime.datetime(2019, 12, 1)
    datetime2 = datetime.datetime(2020, 1, 8)
    factors = TotalFactors()
    # Statistic_Factor(factors, database, datetime1, datetime2)
    # DiffSignificantTest_TopToBot(factors, database, datetime1, datetime2)
    # DiffSignificantTest_PortfolioToMarket(factors, database, datetime1, datetime2)
    # Plot_FatorPortfolio_ToMarkt(database, "Growth_CAGR_TotalAsset_1Yr", datetime1, datetime2)
    # Plot_FatorPortfolio_ToMarkt(database, "Cap", datetime1, datetime2)
    # Plot_FatorPortfolio_ToMarkt(database, "Growth_CAGR_TotalAsset_1Yr", datetime1, datetime2)
    # Plot_FatorPortfolio_ToMarkt(database, "AccrualsToTotalAsset_lYr", datetime1, datetime2)
    # Plot_FatorPortfolio_ToMarkt(database, "PNetCF_R_LYR", datetime1, datetime2)
    # Plot_FatorPortfolio_ToMarkt(database, "CashEarningToEarning_LYR", datetime1, datetime2)
    # Plot_FatorPortfolio_ToMarkt(database, "Growth_CAGR_EPS_5Yr", datetime1, datetime2)
    # Plot_FatorPortfolio_ToMarkt(database, "Growth_CAGR_ROE_3Yr", datetime1, datetime2)
    # Plot_FatorPortfolio_ToMarkt(database, "Growth_CAGR_TotalRevenue_5Yr", datetime1, datetime2)

    # ---常规风格检验 今年以来---
    datetime1 = datetime.datetime(2019, 1, 1)
    datetime2 = datetime.datetime(2020, 1, 8)
    # Plot_FatorPortfolio(database, "ROE_NetIncome2_TTM_Neutral", datetime1, datetime2)
    # Plot_FatorPortfolio(database, "Growth_CAGR_OperatingCashFlow_5Yr", datetime1, datetime2)
    # Plot_FatorPortfolio(database, "BookToMarket", datetime1, datetime2)
    # Plot_FatorPortfolio(database, "Cap", datetime1, datetime2)
    # Plot_FatorPortfolio(database, "Momentum_20D", datetime1, datetime2)
    # Plot_FatorPortfolio(database, "Bias_20D", datetime1, datetime2)
    # Plot_FatorPortfolio(database, "Volatility_Annually")
    # Plot_FatorPortfolio(database, "CAPMBeta_Annually")
    # Plot_FatorPortfolio(database, "CAPMBeta_Annually")

    # 20191210
    # Plot_FatorPortfolio(database, "AccrualsToTotalAsset_lYr", datetime1, datetime2)
    # Plot_FatorPortfolio(database, "Growth_CAGR_OperatingCashFlow_5Yr", datetime1, datetime2)
    # Plot_FatorPortfolio(database, "CashROIC_LYR", datetime1, datetime2)
    # Plot_FatorPortfolio(database, "Holding_MF_Diff_LF", datetime1, datetime2)
    # Plot_FatorPortfolio(database, "ProfitMargin_NetIncome2", datetime1, datetime2)
    # Plot_FatorPortfolio(database, "Growth_YoY_OperatingProfit1", datetime1, datetime2)
    # Plot_FatorPortfolio(database, "Cap", datetime1, datetime2)

    # 20191125
    # Plot_FatorPortfolio(database, "Growth_CAGR_OperatingCashFlow_5Yr", datetime1, datetime2)
    # Plot_FatorPortfolio(database, "CashROIC_LYR", datetime1, datetime2)
    # Plot_FatorPortfolio(database, "Holding_MF_Diff_LF", datetime1, datetime2)
    # Plot_FatorPortfolio(database, "Holding_MF_RatioDiff_YoY", datetime1, datetime2)
    # Plot_FatorPortfolio(database, "Holding_MF_RatioChg_YoY", datetime1, datetime2)
    # Plot_FatorPortfolio(database, "Growth_CAGR_TotalRevenue_3Yr", datetime1, datetime2)
    # Plot_FatorPortfolio(database, "Cap", datetime1, datetime2)

    # 20191009
    # Plot_FatorPortfolio(database, "Growth_CAGR_ProfitMargin_OperatingProfit_1yr")

    # 20191015
    # datetime1 = datetime.datetime(2019, 1, 1)
    # datetime2 = datetime.datetime(2019, 10, 14)
    # Plot_FatorPortfolio(database, "Growth_CAGR_OperatingCashFlow_5Yr")
    # Plot_FatorPortfolio(database, "Holding_MF_RatioChg_LYR", datetime1, datetime2)
    # Plot_FatorPortfolio(database, "PNetCF_R_LYR")
    # Plot_FatorPortfolio(database, "PFCF_LYR")
    # Plot_FatorPortfolio(database, "PNetCF_LYR")

    #
    # datetime1 = datetime.datetime(2019, 1, 1)
    # datetime2 = datetime.datetime(2019, 10, 21)
    # Plot_FatorPortfolio(database, "Growth_CAGR_OperatingCashFlow_5Yr")
    # Plot_FatorPortfolio(database, "Holding_MF_RatioChg_YoY", datetime1, datetime2)
    # Plot_FatorPortfolio(database, "ProfitMargin_NetIncome2", datetime1, datetime2)
    # Plot_FatorPortfolio(database, "AssetQuality_Index", datetime1, datetime2)

    # 20191028
    datetime1 = datetime.datetime(2019, 1, 1)
    datetime2 = datetime.datetime(2019, 10, 28)
    # Plot_FatorPortfolio(database, "Growth_CAGR_OperatingCashFlow_5Yr", datetime1, datetime2)
    # Plot_FatorPortfolio(database, "Holding_MF_RatioChg_YoY", datetime1, datetime2)
    # Plot_FatorPortfolio(database, "ProfitMargin_NetIncome2", datetime1, datetime2)
    # Plot_FatorPortfolio(database, "Growth_YoY_NetIncome2", datetime1, datetime2)

    # 201911111
    datetime1 = datetime.datetime(2019, 1, 1)
    datetime2 = datetime.datetime(2019, 11, 19)
    # Plot_FatorPortfolio(database, "Growth_CAGR_OperatingCashFlow_5Yr", datetime1, datetime2)
    # Plot_FatorPortfolio(database, "Holding_MF_RatioChg_YoY", datetime1, datetime2)
    # Plot_FatorPortfolio(database, "ProfitMargin_NetIncome2", datetime1, datetime2)
    # Plot_FatorPortfolio(database, "Growth_YoY_OperatingProfit1", datetime1, datetime2)

    # 20191119
    datetime1 = datetime.datetime(2019, 1, 1)
    datetime2 = datetime.datetime(2019, 11, 19)
    # Plot_FatorPortfolio(database, "Growth_CAGR_OperatingCashFlow_5Yr", datetime1, datetime2)
    # Plot_FatorPortfolio(database, "CashROIC_LYR", datetime1, datetime2)
    # Plot_FatorPortfolio(database, "Holding_MF_RatioDiff_YoY", datetime1, datetime2)
    # Plot_FatorPortfolio(database, "Holding_MF_RatioChg_YoY", datetime1, datetime2)
    # Plot_FatorPortfolio(database, "Growth_YoY_OperatingProfit1", datetime1, datetime2)
    # Plot_FatorPortfolio(database, "Cap", datetime1, datetime2)
    # Plot_FatorPortfolio(database, "Holding_MF_Diff_LF", datetime1, datetime2)